<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <style>
    #todo{
      max-width: 600px;
      margin: 20px auto;
      padding: 20px;
      border: 1px solid #333;
      border-radius: 10px;
    }
  </style>
</head>
<body>
  <div id="todo">
    <div class="todo-header row">
      <div class="col-md-10">
        <input type="text" v-model.trim="inputTxt" class="form-control">
      </div>
      <div class="col-md-2">
        <button @click="addTodo" class="btn btn-primary">增加</button>
      </div>
    </div>
    <div class="todo-list" style="margin-top: 20px">
      <ul class="list-group">
        <li v-if="todos.length===0"  class="list-group-item text-center">暂无待办事项</li>
        <li
          v-for="(todo,index) in todos"  
          class="list-group-item">
          {{ todo.content }}
          <div class="pull-right">
            <button class="btn btn-xs btn-danger" @click="delTodo(index)">删除</button>
          </div>
        </li>
        
      </ul>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#todo',
      data: {
        // 与 输入框进行绑定 输入 新增待办事项
        inputTxt: '',
        // 已有待办事项列表
        todos: [
         
        ]
      },
      methods: {
        delTodo(index){
          // 删除待办事项
          if(confirm('您确认删除吗?')){
            this.todos.splice(index, 1);
          }
        },
        addTodo(){
          // 非空 不添加
          if(this.inputTxt === '') {
            return false;
          }
          // 新增待办事项
          this.todos.push({
            content: this.inputTxt,
            finished: false
          })
          this.inputTxt = '';
        }
      }
    })
  </script>
</body>
</html>